<?php

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php'); #加载全站初始化配置文件
require_once(dirname(__FILE__) . '/includes/Response.php');
require_once(dirname(__FILE__) . '/includes/Jpush.php');



//搜索好友接口
if ($_REQUEST['act'] == 'search') {

    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
    $_POST['search_id'] = isset($_POST['search_id']) ? trim($_POST['search_id']) : '';

    $sql ="SELECT * FROM " . $ecs->table('friend') . "WHERE user_id = '" .$_POST['user_id'] . "' AND friend_id = '".$_POST['search_id']."'";
    $friend = $db -> getRow($sql);
    if(empty($pa_id)){
        $sql = "SELECT * FROM " . $ecs->table('user') . "WHERE user_id = '" . $_POST['search_id'] ."'";
        $user = $db -> getRow($sql);
        return Response::json(206,"对方还不是该用户的好友",$user);
    }else{
        if($friend['status'] !=1){
            $sql = "SELECT * FROM " . $ecs->table('user') . "WHERE user_id = '" . $_POST['search_id'] ."'";
            $user = $db -> getRow($sql);
            return Response::json(206,"对方还不是该用户的好友",$user);
        }else{
            $sql = "SELECT * FROM " . $ecs->table('user') . "WHERE user_id = '" . $_POST['search_id'] ."'";
            $user = $db -> getRow($sql);
            return Response::json(207,"对方已经是该用户的好友",$user);
        }
    }

}

//好友申请接口
elseif ($_REQUEST['act'] == 'ask_add_friend'){

    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
    $_POST['friend_id'] = isset($_POST['friend_id']) ? trim($_POST['friend_id']) : '';
    $_POST['hi'] = isset($_POST['hi']) ? trim($_POST['hi']) : '';

    $sql = "SELECT id FROM " . $ecs->table('blacklist') . "WHERE user_id = '" . $_POST['friend_id'] ."' AND black_id = '".$_POST['user_id']."'";
    $black_user = $db -> getOne($sql);
    if (empty($black_user)){
        $sql = "SELECT id FROM " . $ecs->table('ask_add_friend') . "WHERE user_id = '" . $_POST['user_id'] ."' AND friend_id = '".$_POST['friend_id']."' AND status = 0";
        $ask_add_friend = $db -> getOne($sql);
        if(empty($ask_add_friend)) {
            $sql = "insert into " . $ecs->table('ask_add_friend') . " (user_id,firend_id) values ('" . $_POST['user_id'] . "','" . $_POST['friend_id'] . "')";
            $db->query($sql);
            if (mysql_affected_rows()) {
                $send_time = date("Y-m-d H:i:s");
                $sql = "insert into " . $ecs->table('add_friend_message') . " (sender_id,receiver_id,send_time,message_text) values('" . $_POST['user_id'] . "','" . $_POST['friend_id'] . "','" . $send_time . "','" . $_POST['hi'] . "')";
                $db->query($sql);
                if (mysql_affected_rows()) {
                    $pushObj = new Jpush();
                    $sql = "SELECT * FROM ".$ecs->table('add_friend_message')." WHERE receiver_id = " .$_POST['friend_id'] ;
                    $message = $db->getRow($sql);
                    $sql = "SELECT user_name FROM ".$ecs->table('user')." WHERE user_id = " .$_POST['user_id'] ;
                    $sender_name = $db->getOne($sql);
                    $push_content = array(
                        'sender_name' => $sender_name,
                        'message' => $message
                    );
                    $content = json_encode($push_content);
                    /*happyxiaod-start*/
                    /*$sql = "SELECT registration_id FROM ".$ecs->table('registration_id')." WHERE user_id = " .$_POST['friend_id'] ;
                    $registration_id = $db->getOne($sql);
                    $receive = array('registration_id'=>array($registration_id));

                    $result = $pushObj->push('all',$content);

                    if($result){
                        $res_arr = json_decode($result, true);
                        if(isset($res_arr['error'])) {                       //如果返回了error则证明失败
                            echo $res_arr['error']['message'];          //错误信息
                            echo $res_arr['error']['code'];             //错误码
                            return false;
                        }
                    }else{      //接口调用失败或无响应
                        echo '推送接口调用失败或无响应';
                        return false;
                    }
                    */
                     /*happyxiaod-end*/
                    return Response::json(200, '申请发送成功');
                } else {
                    return Response::json(502, '打招呼发送失败');
                }
            } else {
                return Response::json(501, '申请发送失败');
            }
        }else{
            $send_time = date("Y-m-d H:i:s");
            $sql = "UPDATE ". $ecs->table('add_friend_message') ." SET send_time = '".$send_time."' message_text = '".$_POST['hi']."', WHERE sender_id = '". $_POST['user_id'] ."' AND receiver_id = '".$_POST['friend_id']."' AND receive_message_id is NULL";
            $db->query($sql);
            if (mysql_affected_rows()) {
                return Response::json(200, '申请发送成功');
            } else {
                return Response::json(502, '打招呼发送失败');
            }
        }

    }else{
        return Response::json(500,"发送申请失败，该用户已被对方加入黑名单");
    }


}

elseif ($_REQUEST['act'] == 'add_friend') {
    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
    $_POST['friend_id'] = isset($_POST['friend_id']) ? trim($_POST['friend_id']) : '';

    $sql = "SELECT id FROM " . $ecs->table('ask_add_friend') . "WHERE user_id = '" . $_POST['user_id'] . "' AND friend_id = '" . $_POST['friend_id'] . "'AND status = 0";
    $add_friend_id = $db->getOne($sql);
    if (!empty($add_friend_id)) {

        $sql = "UPDATE ". $ecs->table('ask_add_friend') ."SET status = 1 WHERE id = '". $add_friend_id ."'";
        $db->query($sql);
        if (mysql_affected_rows()) {
            $sql = "insert into " . $ecs->table('friend') . " (friend_id,user_id,status) values('" . $_POST['friend_id'] . "','" . $_POST['user_id'] . "','1')";
            $db->query($sql);
            if (mysql_affected_rows()) {
                $sql = "insert into " . $ecs->table('friend') . " (friend_id,user_id,status) values('" . $_POST['user_id'] . "','" . $_POST['friend_id'] . "','1')";
                $db->query($sql);
                if (mysql_affected_rows()) {
                    return Response::json(220,'添加好友成功');
                }else{
                    return Response::json(504,'添加好友失败');
                }
            }else{
                return Response::json(504,'添加好友失败');
            }
        }else{
            return Response::json(503,'修改申请状态失败');
        }
    }else{
        $sql = "insert into " . $ecs->table('ask_add_friend') . " (user_id,friend_id,status) values('" . $_POST['user_id'] . "','" . $_POST['friend_id'] . "','1')";
            $r= $db->query($sql);
            if ($r) {
               return Response::json(201,'发送好友信息成功');
            }
    }

}

elseif ($_REQUEST['act'] == 'add_blacklist') {
    $_POST['user_id'] = isset($_POST['user_id']) ? trim($_POST['user_id']) : '';
    $_POST['black_id'] = isset($_POST['black_id']) ? trim($_POST['black_id']) : '';
    $sql = "SELECT id FROM " . $ecs->table('blacklist') . "WHERE user_id = '" . $_POST['user_id'] ."' AND black_id = '".$_POST['black_id']."'";
    $black_user = $db->getOne($sql);
    if (empty($black_user)) {
        $sql = "insert into " . $ecs->table('blacklist') . " (user_id,black_id) values('" . $_POST['user_id'] . "','" . $_POST['black_id'] . "')";
        $db->query($sql);
        if (mysql_affected_rows()) {
            return Response::json(221,'拉入黑名单成功');
        }else{
            return Response::json(506,'拉入黑名单失败');
        }
    }else{
        return Response::json(505,'对方已在此用户黑名单中，请勿重复添加');
    }
}

/**
 * Created by PhpStorm.
 * User: tuhao
 * Date: 2016/11/8
 * Time: 15:36
 */